package server

import (
	"context"
	pb "wire/api/user"
	"wire/internal/service"

	"gitee.com/go-mod/framework/api"
	json "gitee.com/go-mod/framework/response"
	"github.com/gin-gonic/gin"
)

type UserHTTPServer struct {
	us *service.UserService
}

func NewUserHTTPServer(us *service.UserService) *UserHTTPServer {
	return &UserHTTPServer{
		us: us,
	}
}

// Get
func (s *UserHTTPServer) Get(c *gin.Context) {
	req := &pb.GetRequest{}
	if err := c.ShouldBind(&req); err != nil {
		json.ErrorC(c, err, api.ParamError)
		return
	}

	ctx := context.Background()
	res, err := s.us.Get(ctx, req)

	if err != nil {
		json.Error(c, err)
	} else {
		json.Success(c, res)
	}
}

// List
func (s *UserHTTPServer) List(c *gin.Context) {
	req := &pb.ListRequest{}
	if err := c.ShouldBind(&req); err != nil {
		json.ErrorC(c, err, api.ParamError)
		return
	}

	ctx := context.Background()
	res, err := s.us.List(ctx, req)

	if err != nil {
		json.Error(c, err)
	} else {
		json.Success(c, res)
	}
}
